package com.yami.shop.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.cashregisterapp.AppCashRegisterSystemUser;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;

@Controller
@RequestMapping("/app/appCashRegisterSystemUser")
public class AppCashRegisterSystemUserController {


    @Autowired
    private AppCashRegisterSystemUserService appCashRegisterSystemUserService;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('app:appCashRegisterSystemUser:page')")
    public ResponseEntity<IPage<AppCashRegisterSystemUser>> page(AppCashRegisterSystemUser cashRegisterSystemUser, PageParam<AppCashRegisterSystemUser> page) {
        IPage<AppCashRegisterSystemUser> userIPage = appCashRegisterSystemUserService.page(page, new LambdaQueryWrapper<AppCashRegisterSystemUser>()
                .like(StrUtil.isNotBlank(cashRegisterSystemUser.getName()), AppCashRegisterSystemUser::getName, cashRegisterSystemUser.getName())
                .isNotNull(AppCashRegisterSystemUser::getName)
                .orderByDesc(AppCashRegisterSystemUser::getCreateTime));
        return ResponseEntity.ok(userIPage);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{userId}")
    @PreAuthorize("@pms.hasPermission('app:appCashRegisterSystemUser:info')")
    public ResponseEntity<AppCashRegisterSystemUser> info(@PathVariable("userId") Long userId) {
        AppCashRegisterSystemUser cashRegisterSystemUser = appCashRegisterSystemUserService.getById(userId);
        return ResponseEntity.ok(cashRegisterSystemUser);
    }

    /**
     * 修改
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('app:appCashRegisterSystemUser:update')")
    public ResponseEntity<Void> update(@RequestBody AppCashRegisterSystemUser cashRegisterSystemUser ) {
        cashRegisterSystemUser.setUpdateTime(new Date());
        appCashRegisterSystemUserService.updateById(cashRegisterSystemUser);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('app:appCashRegisterSystemUser:delete')")
    public ResponseEntity<Void> delete(@RequestBody String[] userIds) {
        appCashRegisterSystemUserService.removeByIds(Arrays.asList(userIds));
        return ResponseEntity.ok().build();
    }


    /**
     * 新增
     */
    @PostMapping
    @PreAuthorize("@pms.hasPermission('app:appCashRegisterSystemUser:save')")
    public ResponseEntity<Boolean> save(@RequestBody @Valid AppCashRegisterSystemUser cashRegisterSystemUser) {
        return ResponseEntity.ok(appCashRegisterSystemUserService.save(cashRegisterSystemUser));
    }
}
